/*
  Copyright (c) 2009 Dave Gamble, 2013 HONGLEIMING (formatted for easy use)
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
 
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

#ifndef cJSON__h
#define cJSON__h

#ifdef __cplusplus
extern "C"
{
#endif
 
#define JSON_FALSE 0
#define JSON_TRUE 1
#define JSON_NULL 2
#define JSON_NUMBER 3
#define JSON_STRING 4
#define JSON_ARRAY 5
#define JSON_OBJECT 6
	
#define JSON_IsReference 256

/* The cJSON structure: */
typedef struct json_t {
	struct json_t *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
	struct json_t *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* The type of the item, as above. */

	char *valuestring;			/* The item's string, if type==cJSON_String */
	int valueint;				/* The item's number, if type==cJSON_Number */
	double valuedouble;			/* The item's number, if type==cJSON_Number */

	char *string;				/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} json_t;

typedef struct json_hook_t {
      void *(*malloc_fn)(size_t sz);
      void (*free_fn)(void *ptr);
} json_hook_t;

/* Supply malloc, realloc and free functions to cJSON */
extern void json_init_hooks(json_hook_t* hooks);


/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern json_t *json_parse(const char *value);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
extern json_t *json_parse_ext(const char *value,const char **return_parse_end,int require_null_terminated);

/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char  *json_dump(json_t *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char  *json_dump_raw(json_t *item);
/* Delete a cJSON entity and all subentities. */
extern void   json_destroy(json_t *c);

/* Returns the number of items in an array (or object). */
extern int	  json_array_size(json_t *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern json_t *json_array_item(json_t *array,int item);
/* Get item "string" from object. Case insensitive. */
extern json_t *json_object_item(json_t *object,const char *string);

/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *json_error(void);
	
/* These calls create a cJSON item of the appropriate type. */
extern json_t *json_null(void);
extern json_t *json_true(void);
extern json_t *json_false(void);
extern json_t *json_bool(int b);
extern json_t *json_number(double num);
extern json_t *json_string(const char *string);
extern json_t *json_array(void);
extern json_t *json_object(void);

/* These utilities create an Array of count items. */
extern json_t *json_array_int(int *numbers,int count);
extern json_t *json_array_float(float *numbers,int count);
extern json_t *json_array_double(double *numbers,int count);
extern json_t *json_array_string(const char **strings,int count);

/* Append item to the specified array/object. */
extern void json_array_add(json_t *array, json_t *item);
extern void	json_object_add(json_t *object,const char *string,json_t *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
extern void json_array_addref(json_t *array, json_t *item);
extern void	json_object_addref(json_t *object,const char *string,json_t *item);

/* Remove/Detatch items from Arrays/Objects. */
extern json_t* json_array_detach(json_t *array,int which);
extern void    json_array_delete(json_t *array,int which);
extern json_t* json_object_detach(json_t *object,const char *string);
extern void    json_object_delete(json_t *object,const char *string);
	
/* Update array items. */
extern void json_array_replace(json_t *array,int which,json_t *newitem);
extern void json_object_replace(json_t *object,const char *string,json_t *newitem);

/* Duplicate a cJSON item */
extern json_t *json_dup(json_t *item,int recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */

/* Macros for creating things quickly. */
#define json_object_addnull(object,name)	json_object_add(object, name, json_null())
#define json_object_addbool(object,name,b)	json_object_add(object, name, json_bool(b))
#define json_object_addnum(object,name,n)	json_object_add(object, name, json_number(n))
#define json_object_addstr(object,name,s)	json_object_add(object, name, json_string(s))

/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define json_object_setint(object,val)			((object)?(object)->valueint=(object)->valuedouble=(val):(val))
 
#ifdef __cplusplus
}
#endif

#endif
